<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="设备编号">
              <j-input placeholder="设备编号" v-model="queryParam.deviceId"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="设备序列号">
              <!-- <a-input placeholder="设备序列号" v-model="queryParam.serialNumber"></a-input> -->
              <j-input placeholder="设备序列号" v-model="queryParam.serialNumber"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="设备类型">
              <j-dict-select-tag v-model="queryParam.deviceType" dictCode="camera_type" placeholder="设备类型"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="设备厂商">
              <j-dict-select-tag v-model="queryParam.manufacturerId" dictCode="device_manufacturer" placeholder="设备厂商"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
              <a-form-model-item label="路侧杆件">
                <!-- <a-select v-model="queryParam.poleId" title="请选择路侧杆件" :options="poleOptions" placeholder="请选择路侧杆件"/> -->
                <a-select v-model="queryParam.poleId" title="请选择路侧杆件" placeholder="请选择路侧杆件">
                  <a-select-option :value="undefined">请选择</a-select-option>
                  <a-select-option v-for="(item, key) in poleOptions" :key="key" :value="item.value">
                    <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
                      {{ item.text || item.label }}
                    </span>
                  </a-select-option>
                </a-select>
              </a-form-model-item>
          </a-col>

          <a-col :md="6" :sm="8">
              <a-form-model-item label="所属道路">
                <!-- <a-input-number v-model="model.roadName" placeholder="请选择道路" style="width: 100%" /> -->
                <!-- <a-select v-model="queryParam.roadId" title="请选择所属道路" :options="roadOptions" @change="handleRoadChange" placeholder="请选择所属道路"/> -->
                <a-select v-model="queryParam.roadId" title="请选择所属道路" placeholder="请选择所属道路">
                  <a-select-option :value="undefined">请选择</a-select-option>
                  <a-select-option v-for="(item, key) in roadOptions" :key="key" :value="item.value">
                    <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
                      {{ item.text || item.label }}
                    </span>
                  </a-select-option>
                </a-select>
              </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="8">
              <a-form-model-item label="所属路口">
                <!-- <a-select v-model="queryParam.crossId" title="请选择所属路口" :options="crossOptions" @change="handleCrossChange" placeholder="请选择所属路口"/> -->
                <a-select v-model="queryParam.crossId" title="请选择所属路口" placeholder="请选择所属路口">
                  <a-select-option :value="undefined">请选择</a-select-option>
                  <a-select-option v-for="(item, key) in crossOptions" :key="key" :value="item.value">
                    <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
                      {{ item.text || item.label }}
                    </span>
                  </a-select-option>
                </a-select>
              </a-form-model-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-row :gutter="24">
        <a-col :md="18" :sm="8">
            <a-button @click="viewInList" type="primary" :class="{active: viewMode === 'list'}" icon="ordered-list">列表模式</a-button>
            <a-button @click="viewInMap" type="primary" :class="{active: viewMode === 'map'}" icon="global">地图模式</a-button>
        </a-col>
        

      <a-col :md="6" :sm="8">
        <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
        <a-button type="primary" icon="download" @click="handleExportXls('camera')">导出</a-button>
        <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
          <a-button type="primary" icon="import">导入</a-button>
        </a-upload> -->
        <a-button type="primary" icon="delete" @click="batchDel">删除</a-button>
       </a-col>
      </a-row>
    </div>

    <!-- table区域-begin -->
    <div  v-show="viewMode === 'list'">
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">修改</a>
        </span>
      </a-table>
    </div>
     <!-- 地图视图 -->
    <div v-if="viewMode === 'map'" class="map-container">
       <camera-map-view 
        :queryParams="queryParam"
       
      />
    </div>
    <camera-modal ref="modalForm" @ok="modalFormOk"></camera-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CameraModal from './modules/CameraModal'
  import CameraMapView from './modules/CameraMapView'
  import { getRoadList } from '@/api/basicinfo/road/road'
  import { getCrossList } from '@/api/basicinfo/road/cross'
  import { getPoleList } from '@/api/basicinfo/roadside/pole'

  export default {
    name: 'CameraList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CameraModal,
      CameraMapView
    },
    data () {
      return {
        description: 'roadside_device_camera管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'设备编号',
            align:"center",
            dataIndex: 'deviceId'
          },
          {
            title:'设备序列号',
            align:"center",
            dataIndex: 'serialNumber'
          },
          {
            title:'设备类型',
            align:"center",
            dataIndex: 'deviceType_dictText'
          },
          {
            title:'设备厂商',
            align:"center",
            dataIndex: 'manufacturerId_dictText'
          },
          // {
          //   title:'路口名称',
          //   align:"center",
          //   dataIndex: 'crossName'
          // },
          {
            title:'杆件名称',
            align:"center",
            dataIndex: '',
            customRender:function (t,r,index) {
              // console.log('t: ', t);
              // console.log('r: ', r);
              return t.crossName + '-' + t.polePositionType_dictText;
            }
          },
          {
            title:'道路名称',
            align:"center",
            dataIndex: 'roadName'
          },
          {
            title:'联网状态',
            align:"center",
            dataIndex: 'onlineState_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/basicinfo/roadsideDevice/camera/listByCondition",
          delete: "/basicinfo/roadsideDevice/camera/delete",
          deleteBatch: "/basicinfo/roadsideDevice/camera/deleteBatch",
          exportXlsUrl: "/basicinfo/roadsideDevice/camera/exportXls",
          importExcelUrl: "basicinfo/roadsideDevice/camera/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
        crossOptions: [],
        roadOptions: [],
        poleOptions: [],
        viewMode:'list'
      }
    },
    created() {
      this.getSuperFieldList();
      this.initRoadList(); 
      this.initCrossList();
      this.initPoleList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'serialNumber',text:'设备序列号'})
        fieldList.push({type:'string',value:'manufacturerId',text:'设备厂商'})
        fieldList.push({type:'string',value:'deviceType',text:'设备类型'})
        fieldList.push({type:'string',value:'onlineState',text:'联网状态(0离线  1在线)'})
        fieldList.push({type:'int',value:'crossId',text:'路口id'})
        fieldList.push({type:'int',value:'roadId',text:'道路id'})        // fieldList.push({type:'string',value:'roadName',text:'道路名称'})
        fieldList.push({type:'int',value:'poleId',text:'杆件id'})
        this.superFieldList = fieldList
      },
       //初始化道路字典
      initRoadList(){
        getRoadList().then((res)=>{
          if(res.success){
            this.roadOptions = res.result.map((item,index,arr)=>{
              let c = { label:item.roadName, value:item.id }
              return c;
            })
            // console.log('this.roadOptions: ',this.roadOptions)
          }
        });
      },
      //初始化路口字典
      initCrossList(){
        let param = {};
        this.queryCrossList(param);
      },
      queryCrossList(param){
        getCrossList(param).then((res)=>{
          if(res.success){
            this.crossList = res.result;
            this.crossOptions = res.result.map((item,index,arr)=>{
              let c = { label:item.name, value:item.id}
              return c;
            })
            console.log('this.crossOptions: ',this.crossOptions);
          }
        });
      },
      initPoleList(){
        let param = {};
        this.queryPoleList(param);
      },
      queryPoleList(param){
        getPoleList(param).then((res)=>{
          if(res.success){
            // this.poleOptions = [{ label: '全部', value: 'undefined' }]
            
            this.poleOptions = res.result.map((item,index,arr)=>{
                let c = { label: item.crossName + '-' + item.positionTypeText, value:item.id }
                return c;
              })
            // this.poleOptions.push(resultOptions);
            console.log('this.poleOptions: ',this.poleOptions)
          }
        });
      },
      viewInList(){
        this.viewMode = 'list';
      },
      viewInMap(){
        this.viewMode = 'map';
        // this.$refs.mapView.init();
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .ant-btn .active {
    background-color: #1890ff;
    border-color: #1890ff;
    color: #fff;
  }
  .map-container{
    width: 100%;
    height:600px;
  }
</style>